﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace doxyOutParser.CodeCheck
{
	public class Uv4IncludeDirsParser : IIncludeDirsParser
	{
		private string _uv4projectFile;
		public Uv4IncludeDirsParser(string uv4projectFile)
		{
			//TODO Check the value of string and eventually check if exist or not.
			this._uv4projectFile = uv4projectFile;
		}


		#region IIncludeDirsParser Members

		public string[] GetIncludeDirsTable()
		{
            // Using XML document
            XmlDocument uv4Proj = new XmlDocument();
            uv4Proj.Load(this._uv4projectFile);

            XmlNodeList xmlIncludeElementNodeList = uv4Proj.GetElementsByTagName("IncludePath");
            foreach (XmlNode xmlNode in xmlIncludeElementNodeList)
            {
                if (this.GetFullXPath(xmlNode).Contains("Cads"))
                {
                    string includeDirsData = xmlNode.InnerText;
                    string[] dirsSeparators = { ";" };
                    return includeDirsData.Split(dirsSeparators, StringSplitOptions.RemoveEmptyEntries);
                }
            }
            return null;
    	}

		#endregion

        private string GetFullXPath(XmlNode xmlNode)
        {
            string newFullPath = this.GetParentBrowsePath(xmlNode, xmlNode.Name);

            return newFullPath;
        }

        private string GetParentBrowsePath(XmlNode xmlNode, string xmlNodePath)
        {
            string xmlNewNodePath;
            if (xmlNode.ParentNode != null)
            {
                xmlNewNodePath = this.GetParentBrowsePath(xmlNode.ParentNode, xmlNode.ParentNode.Name + "/" + xmlNodePath);
            }
            else
            {
                xmlNewNodePath = xmlNodePath;
            }
            return xmlNewNodePath;
        }
	}
}
